﻿using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using SharePoint.Security.ConfigAnalyzer.Engine.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharePoint.Security.ConfigAnalyzer.Engine
{
    public class BasicInformation
    {
        public static WebAppInfo GetCentralAdminInfo()
        {
            SPAdministrationWebApplication caWebApp = ObjectHelper.SPAdministrationWeb;

            if (caWebApp == null || caWebApp.Sites == null || caWebApp.Sites.Count == 0)
            {
                throw new InvalidOperationException("Could not find sites in web application");
            }

            var site = caWebApp.Sites[0];

            return new WebAppInfo
            {
                Port = site.Port,
                Url = site.Url
            };
        }

        public static ContentDatabaseInfo GetContentDBInfo()
        {
            SPAdministrationWebApplication caWebApp = ObjectHelper.SPAdministrationWeb;

            if (caWebApp == null || caWebApp.Sites == null || caWebApp.Sites.Count == 0)
            {
                throw new InvalidOperationException("Could not find sites in web application");
            }

            var site = caWebApp.Sites[0];

            var db = site.ContentDatabase;

            if (db == null)
            {
                throw new InvalidOperationException("Could not find content database in central administration web site.");
            }

            return new ContentDatabaseInfo
            {
                Name = db.Name,
                Server = db.Server,
                Status = db.Status.ToString()
            };
        }

        public static List<WebAppInfo> GetWebAppNames()
        {
            var webApps = new List<WebAppInfo>();

            foreach (SPService current in ObjectHelper.Farm.Services)
            {

                SPWebService sPWebService = current as SPWebService;
                if (sPWebService != null)
                {
                    foreach (SPWebApplication current2 in sPWebService.WebApplications)
                    {
                        Uri responseUri = current2.GetResponseUri(SPUrlZone.Default);
                        webApps.Add(new WebAppInfo
                        {
                            DisplayName = current2.Name,
                            Port = responseUri.Port,
                            Url = SPHttpUtility.NoEncode(responseUri)
                        });
                    }
                }
            }

            return webApps;
        }

        public static List<string> GetFarmInfo()
        {
            SPFarm farm = ObjectHelper.Farm;

            var servers = new List<string>();
            foreach (SPServer server in farm.Servers)
            {

                servers.Add(server.DisplayName);

            }

            return servers;
        }
    }

    public class WebAppInfo
    {
        public string Url { get; set; }

        public int Port { get; set; }

        public string DisplayName { get; set; }
    }

    public class ContentDatabaseInfo
    {
        public string Server { get; set; }

        public string Name { get; set; }

        public string Status { get; set; }
    }


}
